package com.dingshi.security.api;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import com.dingshi.security.config.HttpClientConfig;
import com.dingshi.security.net.HttpResult;
import com.dingshi.security.net.HttpResultJson;
import com.dingshi.security.net.NetService;

public class DiaryApi extends BaseApi {

	protected static final String TAG = "DiaryApi";

	public DiaryApi(Context context) {
		super(context);
	}

	public void addDiary(final int requestCode, final Context context,
			final String guardNumber, final String deprNumber,
			final String content) {

		new AsyncTask<Object, Integer, ApiResult<Object>>() {
			@Override
			protected ApiResult<Object> doInBackground(Object... strs) {
				JSONObject obj = new JSONObject();
				try {
					obj.put("s", guardNumber);
					obj.put("d", deprNumber);
					obj.put("c", content);
				} catch (JSONException e2) {
					e2.printStackTrace();
				}
				Log.e(TAG, "request params " + obj.toString());
				ApiResult<Object> apiResult = new ApiResult<Object>();
				apiResult.setResultCode(ApiResult.RESULT_FAIL);
				HttpResultJson result = NetService.getWebServices(
						obj.toString(), context, HttpClientConfig.ADD_DIARY_URL);
				if (result.getResultCode() == HttpResult.RESULT_OK) {
					apiResult.setResultCode(ApiResult.RESULT_OK);
					JSONObject json = result.getJson();
					apiResult.setJsonEntity(json);

				} else {
					apiResult.setFailCode(result.getFailCode());
					apiResult.setFailMessage(result.getFailMessage());
				}
				return apiResult;
			}

			@Override
			protected void onPostExecute(ApiResult<Object> apiResult) {
				if (returnResultListener == null) {
					return;
				}
				if (apiResult.getResultCode() == ApiResult.RESULT_OK) {
					returnResultListener.onReturnSucceedResult(requestCode,
							apiResult);
				} else {
					returnResultListener.onReturnFailResult(requestCode,
							apiResult);
				}
			}

			@Override
			protected void onPreExecute() {
				if (returnResultListener == null) {
					return;
				}
				returnResultListener.onReturnLoading(requestCode);
				super.onPreExecute();
			}

		}.execute("");
	}
	public void getDiaryList(final int requestCode, final Context context,
			final String guardNumber, final String deprNumber) {
		
		new AsyncTask<Object, Integer, ApiResult<Object>>() {
			@Override
			protected ApiResult<Object> doInBackground(Object... strs) {
				JSONObject obj = new JSONObject();
				try {
					obj.put("s", guardNumber);
					obj.put("d", deprNumber);
					obj.put("t", "0");
				} catch (JSONException e2) {
					e2.printStackTrace();
				}
				Log.e(TAG, "request params " + obj.toString());
				ApiResult<Object> apiResult = new ApiResult<Object>();
				apiResult.setResultCode(ApiResult.RESULT_FAIL);
				HttpResultJson result = NetService.getWebServices(
						obj.toString(), context, HttpClientConfig.GET_DIARY_URL);
				if (result.getResultCode() == HttpResult.RESULT_OK) {
					apiResult.setResultCode(ApiResult.RESULT_OK);
					JSONObject json = result.getJson();
					apiResult.setJsonEntity(json);
					
				} else {
					apiResult.setFailCode(result.getFailCode());
					apiResult.setFailMessage(result.getFailMessage());
				}
				return apiResult;
			}
			
			@Override
			protected void onPostExecute(ApiResult<Object> apiResult) {
				if (returnResultListener == null) {
					return;
				}
				if (apiResult.getResultCode() == ApiResult.RESULT_OK) {
					returnResultListener.onReturnSucceedResult(requestCode,
							apiResult);
				} else {
					returnResultListener.onReturnFailResult(requestCode,
							apiResult);
				}
			}
			
			@Override
			protected void onPreExecute() {
				if (returnResultListener == null) {
					return;
				}
				returnResultListener.onReturnLoading(requestCode);
				super.onPreExecute();
			}
			
		}.execute("");
	}
}
